﻿/*************************************************************************
 *  Copyright © 2023-2030 Administrator. All rights reserved.
 *------------------------------------------------------------------------
 *  公司：DefaultCompany
 *  项目：CustomBuildProcess
 *  文件：PackageInitializer.cs
 *  作者：Administrator
 *  日期：2024/11/20 22:10:41
 *  功能：Nothing
*************************************************************************/

using System.IO;
using UnityEditor;
using UnityEngine;

namespace CustomBuildProcess.Editor
{
    [InitializeOnLoad]
    public class PackageInitializer
    {
        static PackageInitializer()
        {
            EditorApplication.delayCall += OnEditorLoad;
        }

        private static void OnEditorLoad()
        {
            CreateBuildInfoResources();

            CreateResources<CustomBuildProcessConfig>("CustomBuildProcessConfig");
        }

        private static void CreateBuildInfoResources()
        {
            try
            {
                if (!Directory.Exists(PathManager.BuildDataPath))
                {
                    Directory.CreateDirectory(PathManager.BuildDataPath);
                }

                if (!File.Exists(PathManager.BuildConfigPath))
                {
                    CreateAsset<BuildInfoConfig>(PathManager.BuildConfigPath);
                    Debug.Log($"No BuildSettingConfig.asset file found. Creating default at {PathManager.BuildConfigPath}");
                }

                AssetDatabase.Refresh();
            }
            catch (System.Exception ex)
            {
                Debug.LogError($"Creating BuildSettingConfig.asset failed: {ex.Message}");
            }
        }

        private static void CreateResources<T>(string fileName) where T : ScriptableObject
        {
            string assetFilePath = Path.Combine(PathManager.PackageLocalPath, "Editor/Resources", $"{fileName}.asset");
            // 创建临时资源
            string tempAssetPath = Path.Combine("Assets", $"{fileName}.asset");

            if (!File.Exists(assetFilePath))
            {
                CreateAsset<T>(tempAssetPath);

                try
                {
                    // 确保目标路径的目录存在
                    string targetDirectory = Path.GetDirectoryName(assetFilePath);
                    if (!Directory.Exists(targetDirectory))
                    {
                        Directory.CreateDirectory(targetDirectory);
                    }

                    // 将资源从 Assets 拷贝到包路径
                    File.Move(tempAssetPath, assetFilePath);

                    // 刷新资源数据库，确保资源被正确加载
                    AssetDatabase.Refresh();
                }
                catch (System.Exception ex)
                {
                    Debug.LogError($"Creating {fileName}.asset failed: {ex.Message}");
                }
            }
        }

        public static void CreateAsset<T>(string assetPath) where T : ScriptableObject
        {
            T asset = ScriptableObject.CreateInstance<T>();
            AssetDatabase.CreateAsset(asset, assetPath);
            AssetDatabase.SaveAssets();
        }
    }
}

